<?php
class Registration_IndexController extends Zendvn_Controller_Action{
	
	public function init(){
		parent::init();
		$this->view->main = TRUE;
		$this->view->module = "landpage";
		
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		
		if (isset($data))
		{
			
		}
		else 
		{
			$this->_redirect('/registration/auth/login');
		}
		
	}
	
	public function indexAction(){

		$follower = @$_GET["follower"];
		
		$this->view->follower = $follower;
		
		$auth = new Registration_Model_Auth();
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();

		$page = 1;
		
		$pagesize = 10;
		
		$arr = array(				
			"page"=> $page,
			"pagesize"=> $pagesize,
			"userid"=> @$data->id,
																
		);
		
		$this->view->list_user = $auth->listItem($arr);
		
		
		if($this->getRequest()->isPost()){    
    		
			
			
    		$arrParam['id'] 		=  $data->id;  		
    		$arrParam['company'] 	= @$_POST["txt_company"];
			$arrParam['school'] 	= @$_POST["txt_school"];
			$arrParam['home'] 		= @$_POST["txt_home"];
			$arrParam['interest'] 		= @$_POST["txt_interest"];
			$arrParam['displayname'] 		= "";
			$arrParam['password'] 		= "";
			$arrParam['displayname'] 		= "";
			$arrParam['avatar'] 		= "";
			$arrParam['cover'] 		= "";
			
			
			if(@$_POST["txt_company"] != "" || @$_POST["txt_school"] != "" || @$_POST["txt_home"] != "" || @$_POST["txt_interest"] != "" ){
				

				$auth->updateProfile($arrParam);
				
			}
			
			
				

			$this->_redirect('/registration/?follower=true');
    		
    	}
    	
	}
	
	public function chanelAction(){
		
		$idchanel = @$_POST["chanel"];		
		$chanel = new Chanel_Model_Index();
		
		if($idchanel != ""){
			$this->view->chanel_item = $chanel->getChanelById($idchanel);
		}
		
		if($this->getRequest()->isPost()){    
    		$storage = new Zend_Auth_Storage_Session();
			$data = $storage->read();
			
			$arrParam['name'] 	= @$_POST["txt_name"];		  
    		$arrParam['userid'] 		=  $data->id;  		
    		$arrParam['create_date'] 	= date('Y-m-d h:i:s',time());
			$arrParam['description'] 	= @$_POST["txt_des"];
			$arrParam['type'] 		= @$_POST["type"];
			$arrParam['logo'] 		="";
			$arrParam['location'] 		= @$_POST["txt_location"];
			$arrParam['cover'] 		= "";
			
			
			
			if ($idchanel != ""){
				$arrParam['id'] 		= $idchanel;
				$chanel->updateChanel($arrParam);
				
			}
			else{
					
				$chanelid = $chanel->insertChanel($arrParam);

				$this->_redirect('/chanel/?id='.$chanelid);
				
			}
			
    	}
		
	}
	
	public function uploadcoverAction(){
		
		if($this->getRequest()->isPost()){
		
			$this->view->main = FALSE;
			
			
				
				$storage = new Zend_Auth_Storage_Session();
				$data = $storage->read();
	    		$auth = new Registration_Model_Auth();
				$upload = new Zend_File_Transfer_Adapter_Http();
				
				$path = realpath(APPLICATION_PATH."/../upload/");
				
				$upload->setDestination($path);
				
				$file = $upload->getFileInfo();
				 
				$file_name = $upload->getFilename();
				  
				preg_match("/\.([^\.]+)$/", $file_name, $matches); 
		        $file_ext = $matches[1];
		        $file_name = time() . '.' . $file_ext;
		        $upload->addFilter('Rename', $path .'/' . $file_name);
				
				if (!$upload->isValid()) 
				{
				    throw new Exception('Bad image data: '.implode(',', $upload->getMessages()));
				}
				
				try {
					
			        $upload->receive();		        
			        
			        
			        				
					
					$arr = array(
			        	'id' 		=>  $data->id,  		
			        'displayname' 	=> "",
			        'password' 	=> "",
			        'avatar' 	=> "",
			        'company' 	=> "",
					'school' 	=> "",
					'home' 		=> "",
					'interest' 		=> "",
					'cover' 		=> $file_name,	
					);
					
					$id = $auth->updateProfile($arr);
					
					echo $file_name;
				        
				} 
				catch (Zend_File_Transfer_Exception $e) 
				{
				        throw new Exception('Bad image data: '.$e->getMessage());
				}
				
			
			
				
			
		}
		
	}
	
	
	
}